package cn.gl.usr.service.impl;

import cn.gl.basic.exception.BusinessException;
import cn.gl.basic.util.Md5Utils;
import cn.gl.basic.util.StrUtils;
import cn.gl.usr.domain.Logininfo;
import cn.gl.usr.domain.User;
import cn.gl.usr.mapper.LogininfoMapper;
import cn.gl.usr.mapper.UserMapper;
import cn.gl.usr.service.IUserService;
import cn.gl.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void phoneRegister(Map<String, String> map) {
        //将前端参数赋值
        String phone = map.get("phone");
        String phoneCode = map.get("phoneCode");
        String pwd = map.get("password");
        String rePwd = map.get("passwordRepeat");
        //校验
        //校验空值
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(phoneCode)
                || StringUtils.isEmpty(pwd) || StringUtils.isEmpty(rePwd)) {
            throw new BusinessException("信息不能为空!!!");
        }
        //2.密码是否一致
        if (!pwd.equals(rePwd)) {
            throw new BusinessException("两次密码不一致!!!");
        }
        //3.手机验证码是否过期，是否正确
        Object obj = redisTemplate.opsForValue().get("register:" + phone);
        if (obj == null) {
            throw new BusinessException("手机验证码过期!!!");
        }
        //redis中手机验证码的存储格式【业务键:手机号码 - 验证码:时间戳】
        if (!phoneCode.equals(obj.toString().split(":")[0])) {
            throw new BusinessException("手机验证码错误!!!");
        }
        //二：添加到数据库
        //当将一个对象转换成另外一个对象的时候，而且代码量的时候就用obj2Obj
        User user = map2User(map);
        Logininfo logininfo = user2Logininfo(user);
        //1.先添加t_logininfo
        logininfoMapper.insert(logininfo);
        //2.再添加t_user
        user.setLogininfoId(logininfo.getId());
        userMapper.insert(user);
    }

    @Override
    public void emailRegister(Map<String, String> map) {
        String email = map.get("email");
        String emailpassword = map.get("emailpassword");
        String emailpasswordRepeat = map.get("emailpasswordRepeat");
        String emailCode = map.get("emailCode");
        //校验空值
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(emailpassword)
                || StringUtils.isEmpty(emailpasswordRepeat) || StringUtils.isEmpty(emailCode)) {
            throw new BusinessException("信息不能为空!!!");
        }
        //2.密码是否一致
        if (!emailpassword.equals(emailpasswordRepeat)) {
            throw new BusinessException("两次密码不一致!!!");
        }
        //3.邮箱验证码是否过期，是否正确
        Object obj = redisTemplate.opsForValue().get("register:" + email);
        if (obj == null) {
            throw new BusinessException("邮箱验证码过期!!!");
        }
        //redis中手机验证码的存储格式【业务键:手机号码 - 验证码:时间戳】
        if (!emailCode.equals(obj.toString().split(":")[0])) {
            throw new BusinessException("邮箱验证码错误!!!");
        }
        //二：添加到数据库
        //当将一个对象转换成另外一个对象的时候，而且代码量的时候就用obj2Obj
        User user = maps2Users(map);
        Logininfo logininfo = users2Logininfos(user);
        //1.先添加t_logininfo
        logininfoMapper.insert(logininfo);
        //2.再添加t_user
        user.setLogininfoId(logininfo.getId());
        userMapper.insert(user);


    }


    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        //根据属性同名原则：
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);//用户手机号注册
        logininfo.setDisable(1);//手机注册默认启用
        return logininfo;
    }

    private User map2User(Map<String, String> map) {
        User user = new User();
        String phone = map.get("phone");
        user.setUsername(phone);
        user.setPhone(phone);
        //生成32位随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //加密加盐
        String md5Pwd = Md5Utils.encrypByMd5(map.get("password") + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        user.setState(1);
        return user;
    }

    private Logininfo users2Logininfos(User user) {
        Logininfo logininfo = new Logininfo();
        //根据属性同名原则：
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);//用户手机号注册
        logininfo.setDisable(1);//手机注册默认启用
        return logininfo;
    }

    private User maps2Users(Map<String, String> map) {
        User user = new User();
        String email = map.get("email");
        user.setEmail(email);
        //生成32位随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //加密加盐
        String md5Pwd = Md5Utils.encrypByMd5(map.get("emailpassword") + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        user.setState(1);
        return user;
    }

    //自研重构

    //添加
    @Override
    public void insert(User user) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(user.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);

        //先添加logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
        logininfo.setDisable(1);
        logininfoMapper.insert(logininfo);//获取到自增ID

        //添加user的数据
        user.setLogininfoId(logininfo.getId());
        super.insert(user);
    }
    //删除
    @Override
    public void delete(Long id) {
        //先查询登陆信息id
        User user = userMapper.laodOne(id);

        //删除user
        super.delete(id);

        //删除logininfo
        logininfoMapper.delete(user.getLogininfoId());
    }
    //修改


    @Override
    public void update(User user) {//前端传过来的user是没有登陆信息id
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(user.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);

        User oldUser = userMapper.laodOne(user.getId());//loginfo_id

        //修改user信息
        user.setLogininfoId(oldUser.getLogininfoId());
        super.update(user);

        //修改登陆信息
        Logininfo oldLogininfo = logininfoMapper.laodOne(oldUser.getLogininfoId());
        BeanUtils.copyProperties(user,oldLogininfo);
        //设置会以前的id - 进行修改
        oldLogininfo.setId(oldUser.getLogininfoId());
        logininfoMapper.update(oldLogininfo);
    }


}
